package com.odm.communitymanage.ui.mine.MySeekHelp

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.NetCallBack
import com.odm.communitymanage.core.bean.SeekHelpData
import com.odm.communitymanage.core.net.ApiService
import com.odm.communitymanage.ui.ImageDetailActivity
import com.odm.communitymanage.util.GlideUtils
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_my_seek_help_detail.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 我的求助详情  页面
 * @author: ODM
 * @date: 2020/4/18
 */
class MySeekHelpDetailFragment: BaseFragment() {


    private var disposable : Disposable?= null

    //当前未解决求助事件的id
    private var helpId  = -1

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        registerLiveEvent()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }


    override fun initViews() {
        //"已解决按钮"的点击事件
        btnSolved.setOnClickListener {
            postSeekHelpResolved()
        }
    }

    override fun getLayoutId(): Int {

        return R.layout.fragment_my_seek_help_detail
    }


    private fun registerLiveEvent(){
        LiveEventBus.get(Constants.EVENT_MY_HELP_DETAIL , SeekHelpData::class.java)
            .observeSticky(this  , object : Observer<SeekHelpData> {
                override fun onChanged(t: SeekHelpData?) {
                    loadMySeekHelpDetail(t)
                }
            })
    }


    private fun loadMySeekHelpDetail(data : SeekHelpData ?){
        tvDescription_MySeekHelpDetail.text = data?.content
        tvRemark_MySeekHelpDetail.text  = data?.remarks
        when(data?.status){
            0 ->{
                //说明还没解决，显示 “已解决”按钮 ，隐藏已解决logo
                btnSolved.visibility = View.VISIBLE
                ivSolved_MySeekHelpDetail.visibility = View.GONE
                helpId = data.id
            }
            1->{
                //说明已经解决，隐藏 “已解决”按钮,显示已解决logo
                btnSolved.visibility = View.GONE
                ivSolved_MySeekHelpDetail.visibility = View.VISIBLE
            }
        }
        
        //加载图片 ,imgs 可能返回 null
        data?.imgs?.let {
            for(url in data.imgs){
                var target : ImageView ?= ivPhoto_0_MySeekHelpDetail
                if(ivPhoto_0_MySeekHelpDetail.visibility != View.VISIBLE){
                    target = ivPhoto_0_MySeekHelpDetail
                }else if(ivPhoto_1_MySeekHelpDetail.visibility != View.VISIBLE) {
                    target = ivPhoto_1_MySeekHelpDetail
                }
                target?.apply {
                    visibility = View.VISIBLE
                    //加载图片
                    GlideUtils
                        .getInstance()
                        .loadImageUrl(requireContext(),url ,target)
                    //点击事件，跳转加载大图Activity
                    setOnClickListener {
                        LiveEventBus
                            .get(Constants.IMAGE_URL)
                            .post(url)
                        ActivityUtils.startActivity(ImageDetailActivity::class.java)
                    }
                }
            }
        }
    }


    /*
     * 对当前 未解决的 求助事件 ，提交 已解决的 post请求
     */
    private fun postSeekHelpResolved(){
        if(helpId != -1){
            disposable = RxHttp.postJson(ApiService.COMPLETE_MY_SEEK_HELP)
                .add("id",helpId)
                .asClass(NetCallBack::class.java)
                .subscribe({
                    it?.let {
                        if(it.code == 1){
                            showSuccessSnack(getString(R.string.post_success))
                            pop()
                        }else {
                            ToastUtils.showShort(it.message)
                        }
                    }
                },{
                    it?.let {
                        ToastUtils.showShort(it.message)
                        LogUtils.e(it.message)
                    }

                })
        }
    }
}